<template>
  <div>
    <el-table :data="evaluations" stripe class="table1">
      <el-table-column prop="evaId" label="评价ID" width="100"/>
      <el-table-column prop="taskId" label="任务ID"/>
      <el-table-column prop="maintenanceRecordsId" label="维修记录ID"/>
      <el-table-column prop="userNo" label="用户编号"/>
      <el-table-column prop="evaTime" label="评价时间"/>
      <el-table-column prop="rating" label="评分"/>
      <el-table-column prop="comments" label="评论"/>
      <el-table-column label="操作" width="150">
        <template v-slot="scope">
          <el-button @click="showEditDialog(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button type="danger" @click="deleteEvaluation(scope.row.evaId)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加评价对话框 -->
    <el-dialog title="添加评价" :visible.sync="addDialogVisible">
      <el-form :model="newEvaluation" label-width="120px">
        <el-form-item label="任务ID">
          <el-input v-model="newEvaluation.taskId"/>
        </el-form-item>
        <el-form-item label="维修记录ID">
          <el-input v-model="newEvaluation.maintenanceRecordsId"/>
        </el-form-item>
        <el-form-item label="用户编号">
          <el-input v-model="newEvaluation.userNo"/>
        </el-form-item>
        <el-form-item label="评价时间">
          <el-date-picker v-model="newEvaluation.evaTime" type="datetime" format="yyyy-MM-dd HH:mm:ss"/>
        </el-form-item>
        <el-form-item label="评分">
          <el-input v-model="newEvaluation.rating"/>
        </el-form-item>
        <el-form-item label="评论">
          <el-input type="textarea" v-model="newEvaluation.comments"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEvaluation">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑评价对话框 -->
    <el-dialog title="编辑评价" v-model="editDialogVisible">
      <el-form :model="currentEvaluation" label-width="120px">
        <el-form-item label="任务ID">
          <el-input v-model="currentEvaluation.taskId"/>
        </el-form-item>
        <el-form-item label="维修记录ID">
          <el-input v-model="currentEvaluation.maintenanceRecordsId"/>
        </el-form-item>
        <el-form-item label="用户编号">
          <el-input v-model="currentEvaluation.userNo"/>
        </el-form-item>
        <el-form-item label="评价时间">
          <el-date-picker v-model="currentEvaluation.evaTime" type="datetime" format="yyyy-MM-dd HH:mm:ss"/>
        </el-form-item>
        <el-form-item label="评分">
          <el-input v-model="currentEvaluation.rating"/>
        </el-form-item>
        <el-form-item label="评论">
          <el-input type="textarea" v-model="currentEvaluation.comments"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateEvaluation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { toRaw } from 'vue';

export default {
  data() {
    return {
      evaluations: [],
      addDialogVisible: false,
      editDialogVisible: false,
      newEvaluation: {
        taskId: '',
        maintenanceRecordsId: '',
        userNo: '',
        evaTime: '',
        rating: '',
        comments: ''
      },
      currentEvaluation: {}
    };
  },
  methods: {
    fetchEvaluations() {
      axios.get('/api/service-evaluation/qq')
          .then(response => {
            if (Array.isArray(response.data)) {
              this.evaluations = response.data.map(item => ({
                evaId: item.evaId,
                taskId: item.taskId,
                maintenanceRecordsId: item.maintenanceRecordsId,
                userNo: item.userNo,
                evaTime: item.evaTime,
                rating: item.rating,
                comments: item.comments
              }));
            } else {
              console.error('Expected an array, but got:', response.data);
            }
          })
          .catch(error => {
            console.error('Failed to fetch evaluations:', error);
          });
    },
    addEvaluation() {
      axios.post('/api/service-evaluation/qq', this.newEvaluation)
          .then(() => {
            this.addDialogVisible = false;
            this.fetchEvaluations();
          })
          .catch(error => {
            console.error('Failed to add evaluation:', error);
          });
    },

    deleteEvaluation(id) {
      axios.delete(`/api/service-evaluation/${id}`)
          .then(() => {
            this.fetchEvaluations();
          })
          .catch(error => {
            console.error('Failed to delete evaluation:', error);
          });
    },

    showEditDialog(evaluation) {
      console.log('Editing:', toRaw(evaluation)); // 确保输出的是普通对象
      this.currentEvaluation = { ...toRaw(evaluation) }; // 复制数据以进行编辑
      this.editDialogVisible = true; // 打开编辑对话框
      console.log('editDialogVisible:', this.editDialogVisible); // 检查状态
    },

    updateEvaluation() {
      axios.put(`/api/service-evaluation/qq/${this.currentEvaluation.evaId}`, this.currentEvaluation)
          .then(() => {
            this.editDialogVisible = false;
            this.fetchEvaluations();
          })
          .catch(error => {
            console.error('Failed to update evaluation:', error);
          });
    },

  },
  mounted() {
    this.fetchEvaluations();
  }
}
</script>

<style scoped>
.el-button {
  margin-right: 5px;
}
.table1{
  margin-left: 20px; /* 根据需要调整 */
}

</style>
